const App = getApp()
import { url } from '../../modelApi/url_config.js'
Page({
    data: {
        statusBarHeight: "",
        region: [],
        recName: '',
        phone: '',
        address: '',
        switch1Checked: true,
        regionObj: {},
        delflag: false,
        addressId: '',
    },
    onLoad: function (options) {
        if (options.id) {
            this.getaddrInfo(options.id);
            this.setData({
                addressId: options.id
            })
            
        }
        this.setData({
            statusBarHeight: App.globalData.navHeight,
            delflag: options.id ? true : false,
        });
    },
    onShow: function () {

    },
    getaddrInfo(id) {
        App.ajax(url.addressDetailUrl, { 'addressId': id }, 'get').then(res => {
            const data = res.data;
            if (+res.code === 0 && data) {
                this.setData({
                    region: [data.province, data.city, data.distrect,],
                    recName: data.recName,
                    phone: data.phone,
                    address: data.address,
                    switch1Checked: +data.isDef === 1
                });
            }
        })
    },
    toast(text) {
        wx.showToast({
            title: text,
            icon: 'none',
            duration: 2000
        })
    },
    saveAddr() {
        if (!this.data.region.length) {
            this.toast('地区选择不能为空！')
            return;
        }
        if (!this.data.recName) {
            this.toast('收货人姓名不能为空！')
            return;
        }
        if (!this.data.phone) {
            this.toast('收货人手机号不能为空！')
            return;
        }
        if (!this.data.address) {
            this.toast('详细地址不能为空！')
            return;
        }
        this.saveFunc()
    },
    saveFunc() {
        const flag = JSON.stringify(this.data.regionObj) === '{}'
        const data = flag ? this.data.region : this.data.regionObj;
        const par = {
            province: flag ? data[0] : data.value[0],
            city: flag ? data[1] : data.value[1],
            distrect: flag ? data[2] : data.value[2],
            address: this.data.address,
            recName: this.data.recName,
            phone: this.data.phone,
            postCode: data.postcode,
            isDef: this.data.switch1Checked ? '1' : '0'
        }
        if (this.data.addressId) {
            par.id = this.data.addressId;
        }
        App.ajax(url.savelist, par, 'POST').then(res => {
            if (+res.code === 0) {
                wx.navigateBack({
                    url: '/pages/address/index'
                })
            }
        })
    },
    bindRegionChange(res) {
        const data = res.detail
        this.setData({
            region: data.value,
            regionObj: data
        })
    },
    switch1Change(e) {
        this.setData({
            switch1Checked: e.detail.value
        })
    },
    recNameInput(e) {
        this.setData({
            recName: e.detail.value
        })
    },
    phoneInput(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    addressInput(e) {
        this.setData({
            address: e.detail.value
        })
    },
    confirmFunc() {
        App.ajax(url.deladdress, { addressId: this.data.addressId }, 'POST').then(res => {
            if (+res.code === 0) {
                wx.navigateBack({
                    url: '/pages/address/index'
                })
            }
        })
    },
    deladdrFunc() {
        const that = this;
        wx.showModal({
            title: '温馨提示',
            content: '亲，您确定要删除收获地址吗？',
            success(res) {
                if (res.confirm) {
                    that.confirmFunc();
                }
            }
        })
    }

})